package org.jackysoft.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.GenericGenerator;
import org.jackysoft.annotations.BID;
import org.jackysoft.entity.AbstractNoIdEntity;

/**
 * 表示产品的基类
 */
@MappedSuperclass
public abstract class AbstractProduct extends AbstractNoIdEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4514719306514390446L;
	public static final Log logger = LogFactory.getLog(AbstractProduct.class);
	protected String code;

	protected String name;

	protected Double price = 0d;

	protected String priceUnit = "";	
	
	protected ProductType itemType = ProductType.常规产品;
	
	public AbstractProduct(){};
	
	public AbstractProduct(String code, String name, Double price,
			String priceUnit) {
		super();		
		this.code = code;
		this.name = name;
		this.price = price;
		this.priceUnit = priceUnit;
	}

	
	/**
	 * 产品编码
	 */
	@Id@BID
	@GeneratedValue(generator = "agn")
	@GenericGenerator(name = "agn", strategy = "assigned")
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * 产品名称
	 */
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 产品价格
	 */
	
	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	/**
	 * 价格单位
	 */
	public String getPriceUnit() {
		return priceUnit;
	}

	public void setPriceUnit(String priceUnit) {
		this.priceUnit = priceUnit;
	}

	
	public void setItemType(ProductType itemType) {
		this.itemType = itemType;
	}
	
	@JSON(serialize = false)
	public ProductType getItemType() {
		return itemType;
	}

	@Override
	public String toString() {
		return code+"-"+name;
	}
    
	@Transient
	public boolean sameAs(Product other){
		if(other==null)return false;
		if(this.code==null || other.code==null)return false;
		return this.code.equalsIgnoreCase(other.code);
		
	}

	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof AbstractProduct)) return false;
		
		return sameAs((Product) obj);
	}

	@Override
	public int hashCode() {
		
		return HashCodeBuilder.reflectionHashCode(this);
	}	
	
}
